﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringManipulation
{
    class Appointment
    {
        private DateTime startDate;
        private DateTime endDate;
        private bool hasConflict;

        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }

        public DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }

        public bool HasConflict
        {
            get { return hasConflict; }
            set { hasConflict = value; }
        }

        public Appointment() { }
    }

    partial class Program
    {
        public static List<Appointment> CreateAppointmentList()
        {
            List<Appointment> appList = new List<Appointment>();

            Appointment newAppointment;
            DateTime initDateTime = DateTime.Now;
            int hourAdd = 0;
            Random rand = new Random();
            for (int i = 0; i < 5; i++)
            {
                newAppointment = new Appointment();
                newAppointment.StartDate = initDateTime.AddHours(hourAdd++);
                newAppointment.EndDate = newAppointment.StartDate.AddMinutes(rand.Next(40, 80));
                newAppointment.HasConflict = false;
                appList.Add(newAppointment);
            }
            return appList;
        }

        public static List<Appointment> MarkConflicts(List<Appointment> appList)
        {
            //for( int j = 0; j < appList.Count; j++)
            //{
            //    for(int i = j+1; i < appList.Count; i++)
            //    {
            //        if((appList[j].StartDate >= appList[i].StartDate && appList[j].StartDate < appList[i].EndDate) || (appList[j].EndDate <= appList[i].EndDate && appList[j].EndDate > appList[i].StartDate))
            //        {
            //            appList[j].HasConflict = true;
            //            appList[i].HasConflict = true;
            //        }
            //    }
            //}

            //list is sorted based on starttime
            int j = 0;
            for (int i = 1; i < appList.Count; i++)
            {
                if (appList[j].EndDate > appList[i].StartDate)
                {
                    appList[j].HasConflict = true; appList[i].HasConflict = true;
                }
                else
                    j = i;
            }
            return appList;
        }
    }
}
